package com.pmsystem.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pmsystem.pojo.Officeman;
import com.pmsystem.pojo.OfficemanShow;
import com.pmsystem.service.OfficemanService;

/**
 * 
* <p>Title: OfficemanController</p>  
* <p>Description:办公室人员信息的控制器 </p>  
* @author 树昌  
* @date 2019年1月25日
 */
@Controller
@RequestMapping("office")
public class OfficemanController {

	@Autowired
	OfficemanService officemanService;
	
	
	
	/**
	 * 
	 * <p>Title: addOfficeman</p>  
	 * <p>Description:添加办公室人员信息的控制器 </p>  
	 * @author 树昌
	 * @param officeman
	 * @return
	 */
	@RequestMapping(value="addofficeman",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addOfficeman(Officeman officeman) {
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row =officemanService.addOfficeman(officeman);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: showOfficeman</p>  
	 * <p>Description:显示办公室人员信息 </p>  
	 * @author 树昌
	 * @param keyword	搜索关键词
	 * @param startRow	页码
	 * @param pageSize	每页显示的记录条数
	 * @return 不做跳转
	 */
	@RequestMapping(value="showofficeman",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showOfficeman(String keyword, Integer startRow, Integer pageSize){
		
		Map<String, Object> map = new HashMap<>();
		List<OfficemanShow> listOfficeman = officemanService.showOfficeman(keyword, startRow, pageSize);
		map.put("listOfficeman", listOfficeman);
		int size = listOfficeman.size();
		map.put("size", size);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: showOfficemanByKey</p>  
	 * <p>Description:根据ID查询一条办公室人员的信息 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@RequestMapping(value="showOfficemanByKey",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showOfficemanByKey(int id){
		
		Map<String, Object> map = new HashMap<>();
		Officeman officeman = officemanService.showOneOfficeman(id);
		map.put("officeman",officeman);
		return map;
	}
	
	
	
	/**
	 * 
	 * <p>Title: deleteOfficeman</p>  
	 * <p>Description:删除一条办公室人员的信息 </p>  
	 * @author 树昌
	 * @param officeman
	 */
	@RequestMapping(value="deleteofficeman",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteOfficeman(int id) {
		
		Map<String, Object> map =new HashMap<>();
		String msg = null;
		Officeman officeman = new Officeman();
		officeman.setOfficemanId(id);
		boolean row = officemanService.deleteOfficeman(officeman);
		if(row) {
			msg ="success";
		}else {
			msg ="error";
		}
		map.put("msg", msg);
		return map;
	}
	
	@RequestMapping(value="deleteAllOfficeman",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteAllOfficeman(int[] id){
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		List<Integer> listID = new ArrayList<>();
		for (int i : id) {
			listID.add(i);
		}
		boolean row = officemanService.deleteAllOfficeman(listID);
		if(row) {
			msg ="success";
		}else {
			msg ="error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	
	@RequestMapping(value="updateofficeman",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateOfficeman(Officeman officeman) {
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row =officemanService.updateOfficeman(officeman);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
}
